/**************************************************************************/
/*                                                                        */
/*  System  Name :  ICIM                                                  */
/*                                                                        */
/*  Description  :  Product Management                                    */
/*                                                                        */
/*  MODIFICATION HISTORY                                                  */
/*    Date     Ver     Name          Description                          */
/* ---------- ----- ----------- ----------------------------------------- */
/* 2017/05/18 N0.00   meijiao.chen      Initial release                   */
/*                                                                        */
/**************************************************************************/

$(document).ready(function() {
    var VAL ={
        NORMAL     : "0000000"  ,
        EVT_USER   : $("#userId").text(),
        T_FIPINQSHT  : "FIPINQSHT",
        T_FBPBISOPE	 : 'FBPBISOPE',
        T_FBPBISTOL  : 'FBPBISTOL',
        T_FBPBISPTH  : 'FBPBISPTH',
        T_FIPINQBOX  : 'FIPINQBOX',
        T_FPPIWIPEQ  : 'FPPIWIPEQ',
        T_FPPPNLOUT  : 'FPPPNLOUT',
        T_FPPMOVEOUT : 'FPPMOVEOUT',
        T_FSPPNLJGE  : 'FSPPNLJGE',
        T_FBPMDLDEF:'FBPMDLDEF',
        T_FSPGRDCHG : "FSPGRDCHG",
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {
        $opeIdSel : $("#opeIdSel"),
        $toolIdSel: $("#toolIdSel"),
        $woIdSpan  : $("#woIdSpan"),
        $prdStatSpan : $("#prdStatSpan"),
        $prdQtySpan  : $("#prdQtySpan"),
        $mdlIdSpan   : $("#mdlIdSpan"),
        $opeIdSpan   : $("#opeIdSpan"),
        $opeVerSpan  : $("#opeVerSpan"),
        $pathIdSpan  : $("#pathIdSpan"),
        $pathVerSpan : $("#pathVerSpan"),
        $logonTimeSpan : $("#logonTimeSpan"),
        $okCheckBox  : $("#okCheckBox"),
        $okCrrIdTxt  : $("#okCrrIdTxt"),
        OKslotNoSel  : $("#OKslotNoSel"),
        $OKmaxCntSpan: $("#OKmaxCntSpan"),
        $OKcurCntSpan: $("#OKcurCntSpan"),
        $ngCheckBox  : $("#ngCheckBox"),
        $ngCrrIdTxt  : $("#ngCrrIdTxt"),
        $NGslotNoSel : $("#NGslotNoSel"),
        $NGmaxCntSpan: $("#NGmaxCntSpan"),
        $NGcurCntSpan: $("#NGcurCntSpan"),
        $OthCheckBox : $("#OthCheckBox"),
        $OthCrrIdTxt : $("#OthCrrIdTxt"),
        $OthslotNoSel: $("#OthslotNoSel"),
        $OthmaxCntSpan: $("#OthmaxCntSpan"),
        $OthcurCntSpan: $("#OthcurCntSpan"),
        $portIdSel    : $("#portIdSel"),
        $remarkTxt    : $("#remarkTxt"),

        buttons:{
            $query_btn : $("#query_btn"),
            $prdMoveInBox_btn : $("#prdMoveInBox_btn"),
            $moveOut_btn   : $("#moveOut_btn"),
            $clear_btn     : $("#clear_btn"),
            $OKbtn         : $("#OKbtn"),
            $NGbtn         : $("#NGbtn"),
            $Othbtn        : $("#Othbtn"),
            $boxInfoBtn    : $("#boxInfoBtn")
        },
        grids : {
            $shtInprListGrd : $("#shtInprListGrd"),
            $bomListGrd : $("#bomListGrd"),
            $defectListGrd  : $("#defectListGrd"),
            $boxOutListGrd  : $("#boxOutListGrd")
        },
        dialogs : {
            $remarkDialog : $("#remarkDialog"),
            $remarkDialog_remarkTxt : $("#remarkDialog_remarkTxt"),
            $remarkDialog_sureBtn : $("#remarkDialog_sureBtn"),

            $judgeDialog : $("#judgeDialog"),
            $judgeDialog_GradeSel : $("#judgeDialog_GradeSel"),
            $judgeDialog_sureBtn  : $("#judgeDialog_sureBtn")
        }

    };
    var lastrow, lastcell,lastId;
    var judgeDialogFnc = function(){
        // var id = obj.id;
        // var rowId = id.substring(3,id.length);
        var rowIds = domObj.grids.$shtInprListGrd.jqGrid("getGridParam","selarrrow");
        if(rowIds.length >1){
            showErrorDialog("","只能单片转入!");
            return false;
        }
        var rowData = domObj.grids.$shtInprListGrd.jqGrid(
            "getRowData", rowIds[0]);
        var prd_seq_id = rowData.prd_seq_id;
        var chip_grade,mdl_id_fk ;
        var inObj = {
            trx_id :VAL.T_FIPINQSHT,
            action_flg:'T',
            prd_seq_id : prd_seq_id
        };
        var outObj = comTrxSubSendPostJson(inObj);
        if(outObj.rtn_code === VAL.NORMAL){
            if(outObj.prd_tbl==undefined){
                showErrorDialog("", "当前玻璃代码有误");
                return false;
            }
            chip_grade = outObj.prd_tbl.chip_grade;
            mdl_id_fk = outObj.prd_tbl.mdl_id_fk;
        }
        var grades = chip_grade.split("");
        var infoCM=[];
        var x = outObj.prd_tbl.x_axis_cnt_fk;
        var y =  outObj.prd_tbl.y_axis_cnt_fk;
        for(var i=1;i<=x;i++){
            var str_pos={};
            str_pos = {name:"pos_id_"+i  ,index:"pos_id_"+i   ,label : i+"Location",width :"150px" ,align:"left" ,sortable:false,editable:true};
            infoCM.push(str_pos);
        }
        setLayoutGridInfo(infoCM);
        var a = new Array();
        for(var j=1;j<=y;j++){
            a[j-1] = [];
            for(var i=1;i<=x;i++){
                if(chip_grade[(j-1)*x+i-1]!=undefined){
                    a[j-1][i-1]= grades[(j-1)*x+i-1];
                }else{
                    a[j-1][i-1]="";
                }
            }
        }
        for(var j=1;j<=y;j++){
            var str="";
            for(var i=1;i<=x;i++){
                str +="\"pos_id_"+i+"\":\""+a[j-1][i-1]+"\"";
                if( i != x ){
                    str += ",";
                }
            }
            var ss = "{" + str + "}";
            var rowData = eval("("+ss+")");
            $("#layoutGrd").jqGrid("addRowData", j, rowData);
        }
        domObj.dialogs.$judgeDialog.modal({
            backdrop : 'static',
            keyboard : false,
            show : false
        });
        domObj.dialogs.$judgeDialog_sureBtn.unbind('click');
        domObj.dialogs.$judgeDialog_sureBtn.bind('click', function(){
            btnFunc.judgeDialog.judgeFunc(prd_seq_id,mdl_id_fk);
        });
        domObj.dialogs.$judgeDialog.modal("show");
    };
    var controlsFunc = {
        initShtInprGirdInfo : function() {
            var itemInfoCM = [
                // {name: 'judgeItem'   , index: 'judgeItem'    , label: JUDGE_TAG,     sortable: false  ,width: 60 },
                {
                    name  : 'slot_no',
                    index : 'slot_no',
                    label : SLOT_NO_TAG,
                    width : 50
                },{
                    name : 'prd_seq_id',
                    index : 'prd_seq_id',
                    label : PRD_SEQ_ID_TAG,
                    width : 200
                },
                {
                    name  : 'prd_grade',
                    index : 'prd_grade',
                    label : PRD_GRADE_TAG,
                    width : 80
                },{
                    name  : 'pv_box_id_fk',
                    index : 'pv_box_id_fk',
                    label : LOGON_BOX_TAG,
                    width : 100
                }, {
                    name  : 'prd_stat',
                    index : 'prd_stat',
                    label : "玻璃状态",
                    width : 80
                }, {
                    name  : 'prd_qty',
                    index : 'prd_qty',
                    label : "玻璃数量",
                    width : 80
                }];
            domObj.grids.$shtInprListGrd.jqGrid({
                datatype : "local",
                autoheight : true,
                mtype : "POST",
                height : 370,
                autowidth : true,//宽度根据父元素自适应
                shrinkToFit : true,
                scroll : true,
                resizable : true,
                rownumbers : true,
                loadonce : true,
                viewrecords : true,
                colModel : itemInfoCM,
                multiselect : true,
                multiboxonly : true,
                pager : "shtInprListPg",
                gridComplete : function() {
                    var gridPager, pageLen;
                    gridPager = $(this).jqGrid("getGridParam", "pager");
                    if (gridPager.length < 2) {
                        return false;
                    }
                    $("#sp_1_" + gridPager.substr(1, pageLen - 1))
                        .hide();
                    $(".ui-pg-input").hide();
                    $('td[dir="ltr"]').hide();
                    $(gridPager + "_left").hide();
                    $(gridPager + "_center").css({
                        width : 0
                    });
                },
                onSelectRow : function(id) {
                    controlsFunc.getPrdInfo(id);
                },
                loadComplete: function(xhr){
                    controlsFunc.addGridButton();
                }
            })
        },
        initBoxOutGirdInfo : function() {
            var itemInfoCM = [
                {
                    name  : 'slot_no',
                    index : 'slot_no',
                    label : SLOT_NO_TAG,
                    width : 50
                },{
                    name : 'prd_seq_id',
                    index : 'prd_seq_id',
                    label : PRD_SEQ_ID_TAG,
                    width : 200
                }, {
                    name  : 'box_id_fk',
                    index : 'box_id_fk',
                    label : "出账卡匣",
                    width : 100
                }, {
                    name  : 'prd_grade',
                    index : 'prd_grade',
                    label : "玻璃等级",
                    width : 80
                }];
            domObj.grids.$boxOutListGrd.jqGrid({
                datatype : "local",
                autoheight : true,
                mtype : "POST",
                height : 200,
                autowidth : true,//宽度根据父元素自适应
                shrinkToFit : true,
                scroll : true,
                resizable : true,
                rownumbers : true,
                loadonce : true,
                viewrecords : true,
                colModel : itemInfoCM,
                multiselect : true,
                pager : "boxOutListPg",
                gridComplete : function() {
                    var gridPager, pageLen;
                    gridPager = $(this).jqGrid("getGridParam", "pager");
                    if (gridPager.length < 2) {
                        return false;
                    }
                    $("#sp_1_" + gridPager.substr(1, pageLen - 1))
                        .hide();
                    $(".ui-pg-input").hide();
                    $('td[dir="ltr"]').hide();
                    $(gridPager + "_left").hide();
                    $(gridPager + "_center").css({
                        width : 0
                    });
                },
                onSelectRow : function(id) {
//					controlsFunc.getPrdInfo(id);
                }
            })
        },
        initOpeSelect : function(){
            var inObj, outObj;
            var ope_arr = new Array();
            inObj = {
                trx_id      : VAL.T_FBPBISOPE,
                action_flg  : 'L'
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if(outObj.tbl_cnt>0){
                    for(var i=0;i<outObj.tbl_cnt;i++){
                        if(outObj.oary[i].tool_run_mode != "UPK"){
                            ope_arr.push(outObj.oary[i]);
                        }
                    }
                }
                controlsFunc.setOpeSelectDate(ope_arr.length, ope_arr, "ope_id", "ope_ver", "ope_id","tool_run_mode",
                    domObj.$opeIdSel, true);
            }
        },
        initToolSel: function(){
            var opeSelVal = $("#opeIdSel option:selected").val();
            var selectedMode = opeSelVal.split("@")[2];
            var inObj, outObj;
            domObj.$toolIdSel.empty();
            var iary = {
                tool_run_mode : selectedMode,
                tool_stat     : 'RUN',
                unit_typ      : 'MAIN'
            };
            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'H',
                iary		 : [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$toolIdSel, outObj.oary, "tool_id","tool_id","",true);
            }
            domObj.$toolIdSel.select2({
                theme : "bootstrap"
            });
        },
        initPortSel: function(){
            var toolSelVal = $("#toolIdSel option:selected").val();
            var inObj, outObj;
            domObj.$portIdSel.empty();
            if(!toolSelVal){
                return false;
            }
            var iary = {
                tool_id : toolSelVal,
                tool_stat : "RUN,IDLE",
                port_typ: "U"
            }
            inObj = {
                trx_id    : VAL.T_FBPBISTOL,
                action_flg: 'I',
                iary	  : [iary]
            }
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$portIdSel, outObj.oaryB, "port_id","port_id");
            }
            domObj.$portIdSel.select2({
                theme : "bootstrap"
            });

        },
        setOpeSelectDate : function(dataCnt, arr, selVal, selVal2, selTxt, selVal3, queryObj, firstSpace){
            var i, realCnt;

            queryObj.empty();
            realCnt = parseInt( dataCnt, 10);
            if(firstSpace === true){
                queryObj.append("<option ></option>");
            }
            if( realCnt == 1 ){
                queryObj.append("<option value="+ arr[selVal] + "@" + arr[selVal2] +"@"+arr[i][selVal3]+">"+ arr[selTxt] +"</option>");
            }else if( realCnt > 1 ){
                for( i = 0; i < realCnt; i++ ){
                    queryObj.append("<option value="+ arr[i][selVal] + "@" + arr[i][selVal2] + "@"+arr[i][selVal3]+">"+ arr[i][selTxt] +"</option>");
                }
            }
            queryObj.select2({
                theme : "bootstrap"
            });
        },
        setInprPrdList:function(){
            var inObj, outObj;
            var ope_info,ope_id,ope_ver,proc_id;

            var ope_info = domObj.$opeIdSel.val();
            ope_info_ary = ope_info.split("@");
            ope_id = ope_info_ary[0];
            ope_ver = ope_info_ary[1];
            var tool_id = $("#toolIdSel option:selected").val();
            if(!tool_id){
                return false
            }
            domObj.grids.$shtInprListGrd.clearGridData();
            inObj={
                trx_id : VAL.T_FPPIWIPEQ,
                action_flg: 'S',
                cr_ope_id : ope_id,
                cr_ope_ver: ope_ver,
                tool_id   : tool_id
            }
            outObj = comTrxSubSendPostJson(inObj);
            if(outObj.rtn_code == VAL.NORMAL){
                if(outObj.oary_prd.length>0){
                    setGridInfo(outObj.oary_prd, "#shtInprListGrd");
                    controlsFunc.addGridButton();
                    domObj.grids.$boxOutListGrd.clearGridData();
                }
            }
        },
        getPrdInfo:function(id){
            var rowData = domObj.grids.$shtInprListGrd.jqGrid("getRowData", id);
            var inObj, outObj;
            inObj={
                trx_id : VAL.T_FIPINQSHT,
                action_flg : 'Q',
                prd_seq_id : rowData.prd_seq_id
            }
            outObj = comTrxSubSendPostJson(inObj);
            if(outObj.rtn_code == VAL.NORMAL){
                domObj.$woIdSpan.text(outObj.wo_id_fk);
                domObj.$pathIdSpan.text(outObj.cr_path_id_fk);
                domObj.$pathVerSpan.text(outObj.cr_path_ver_fk);
                domObj.$opeIdSpan.text(outObj.cr_ope_id_fk);
                domObj.$opeVerSpan.text(outObj.cr_ope_ver_fk);
                domObj.$mdlIdSpan.text(outObj.mdl_id_fk);
                var logon_timestamp = outObj.logon_timestamp;
                domObj.$logonTimeSpan.text(logon_timestamp.split('.')[0]);
            }
        },
        initControls:function(){
            $(".ok").attr(VAL.DISABLED_ATTR);
            $(".ng").attr(VAL.DISABLED_ATTR);
            $(".other").attr(VAL.DISABLED_ATTR);
            $("input").val("");
            domObj.$okCrrIdTxt.val("");
            domObj.$ngCrrIdTxt.val("");
            domObj.$OthCrrIdTxt.val("");
            domObj.grids.$shtInprListGrd.clearGridData();
            domObj.grids.$boxOutListGrd.clearGridData();
        },
        /**
         * 增加判定按钮
         * 说明:(1) addGridButton :在Grid中添加了一个Button: "判定"
         *     (2) judgeFnc     :点击判定时,调用此函数.
         */
        addGridButton : function(){
            var ids = domObj.grids.$shtInprListGrd.jqGrid('getDataIDs');

            //     for(var i=0; i<ids.length; i++){
            //         var id=ids[i];
            //         var judge = "<button id=" + "'jdg" + ids[i] + "'" + " style='color:#f60' >判定</button>";
            //
            //         domObj.grids.$shtInprListGrd.jqGrid('setRowData', ids[i], { judgeItem: judge});
            //         $("#jdg" +ids[i]).click(function(){
            //             judgeDialogFnc(this);
            //         });
            // }
        }
    };

    var btnFunc={
        okBtnfunc:function(){
            $(".OK").attr(VAL.ENABLED_ATTR);
            $(".ok").attr(VAL.ENABLED_ATTR);
            $(".ng").attr(VAL.DISABLED_ATTR);
            $(".other").attr(VAL.DISABLED_ATTR);
            domObj.$okCrrIdTxt.focus();
        },
        ngBtnfunc:function(){
            $(".ng").attr(VAL.ENABLED_ATTR);
            $(".ok").attr(VAL.DISABLED_ATTR);
            $(".other").attr(VAL.DISABLED_ATTR);
            domObj.$ngCrrIdTxt.focus();
        },
        OthBtnfunc:function(){
            $(".other").attr(VAL.ENABLED_ATTR);
            $(".ok").attr(VAL.DISABLED_ATTR);
            $(".ng").attr(VAL.DISABLED_ATTR);
            domObj.$OthCrrIdTxt.focus();
        },
        swithTofunc:function(){
            judgeDialogFnc();
        },
        moveOutBefore:function(){
            var out_box_id="", tool_id="", port_id="", idLens;
            tool_id = $("#toolIdSel option:selected").val();
            var port_id = $("#portIdSel option:selected").val();

            if(domObj.$okCrrIdTxt.attr("disabled") != "disabled"){
                out_box_id = domObj.$okCrrIdTxt.val().trim();
            }else if(domObj.$ngCrrIdTxt.attr("disabled") != "disabled"){
                out_box_id = domObj.$ngCrrIdTxt.val().trim();
            }else if(domObj.$OthCrrIdTxt.attr("enabled") === "enabled"){
                out_box_id = domObj.$OthCrrIdTxt.val().trim();
            }
            domObj.dialogs.$remarkDialog.modal("hide");
            if(!out_box_id){
                showErrorDialog("","请输入转入箱号！");
                return false;
            }
            if(!tool_id ){
                showErrorDialog("","请选择设备代码！");
                return false;
            }
            if(!port_id ){
                showErrorDialog("","请选择设备端口代码！");
                return false;
            }
            var rowIds = domObj.grids.$boxOutListGrd.getGridParam("reccount");
            if(rowIds == 0){
                showErrorDialog("", "请获取该箱号中准备出账的玻璃信息！");
                return false;
            }
            btnFunc.remarkDialog.remarkShowFnc();
        },
        moveOutfunc:function(){
            var inObj,outObj;
            var out_box_id="", tool_id="", port_id="", idLens;
            tool_id = $("#toolIdSel option:selected").val();
            var port_id = $("#portIdSel option:selected").val();

            if(domObj.$okCrrIdTxt.attr("disabled") != "disabled"){
                out_box_id = domObj.$okCrrIdTxt.val().trim();
            }else if(domObj.$ngCrrIdTxt.attr("disabled") != "disabled"){
                out_box_id = domObj.$ngCrrIdTxt.val().trim();
            }else if(domObj.$OthCrrIdTxt.attr("enabled") === "enabled"){
                out_box_id = domObj.$OthCrrIdTxt.val().trim();
            }
            var remark=domObj.$remarkTxt.val();
            if(!remark){
                showErrorDialog("","请输入备注信息！");
                return false;
            }
            domObj.dialogs.$remarkDialog.modal("hide");

            inObj = {
                trx_id  : VAL.T_FPPMOVEOUT,
                evt_usr : VAL.EVT_USER,
                // evt_usr : "CMJ",
                box_id  : out_box_id,
                tool_id : tool_id,
                port_id : port_id,
                sht_ope_msg : remark,
                opi_ope_flg : "Y"
            };
            outObj = comTrxSubSendPostJson(inObj);
            if(outObj.rtn_code == VAL.NORMAL){
                showSuccessDialog("出账成功");
                domObj.grids.$boxOutListGrd.clearGridData();
                domObj.$remarkTxt.val("");
            }
        },
        clear_func:function(){
            domObj.grids.$bomListGrd.jqGrid("clearGridData");
            $("input[type='text']").val("");
            SelectDom.initWithSpace($("select"),"","");
            // SelectDom.setSelect($("select"),"","");
            $("select").empty();
            controlsFunc.initOpeSelect();
        },

        getBoxInf:function(flag,box_id){
            var inObj,outObj;
            var tool_id = $("#toolIdSel option:selected").val();
            if(box_id == ""){
                showErrorDialog("","请输入转入箱号！");
                return false;
            }
            domObj.grids.$boxOutListGrd.clearGridData();

            domObj.$OKmaxCntSpan.val("");
            domObj.$OKcurCntSpan.val("");

            domObj.$NGmaxCntSpan.val("");
            domObj.$NGcurCntSpan.val("");

            domObj.$OthcurCntSpan.val("");
            domObj.$OthmaxCntSpan.val("");

            inObj={
                trx_id : VAL.T_FIPINQBOX,
                action_flg : "Q",
                box_id : box_id,
            }
            outObj = comTrxSubSendPostJson(inObj);
            if(outObj.rtn_code == VAL.NORMAL){
                if(outObj.box_stat != "INPR" && outObj.box_stat != "EMPT"){
                    showErrorDialog("","箱子状态["+outObj.box_stat+"]不正确，只可以操作空卡匣或者在制程中的卡匣！");
                    domObj.$okCrrIdTxt.val("");
                    domObj.$ngCrrIdTxt.val("");
                    domObj.$OthCrrIdTxt.val("");
                    return false;
                }
                if(outObj.box_stat == "INPR"){
                    if(outObj.oary[0].cr_tool_id_fk.trim() != tool_id){
                        showErrorDialog("","转入箱号对应设备代码["+outObj.oary[0].cr_tool_id_fk.trim()+
                            "]与所选设备代码["+tool_id+"]不符!");
                        domObj.$okCrrIdTxt.val("");
                        domObj.$ngCrrIdTxt.val("");
                        domObj.$OthCrrIdTxt.val("");
                        return false;
                    }
                }
                if(flag == "OK"){
                    domObj.$OKmaxCntSpan.val(outObj.max_cnt);
                    domObj.$OKcurCntSpan.val(outObj.prd_qty);
                    setGridInfo(outObj.oary, "#boxOutListGrd");
                }else if(flag == "NG"){
                    domObj.$NGmaxCntSpan.val(outObj.max_cnt);
                    domObj.$NGcurCntSpan.val(outObj.prd_qty);
                    setGridInfo(outObj.oary, "#boxOutListGrd");
                }else if(flag == "Other"){
                    domObj.$OthmaxCntSpan.val(outObj.max_cnt);
                    domObj.$OthcurCntSpan.val(outObj.prd_qty);
                    setGridInfo(outObj.oary, "#boxOutListGrd");
                }

            }
        },
        remarkDialog : {
            remarkShowFnc : function() {
                domObj.dialogs.$remarkDialog.modal({
                    backdrop : 'static',
                    keyboard : false,
                    show : false
                });
                domObj.dialogs.$remarkDialog_sureBtn.unbind('click');
                domObj.dialogs.$remarkDialog_sureBtn.bind('click', btnFunc.moveOutfunc);
                domObj.dialogs.$remarkDialog.modal("show");
            }
        },
        judgeDialog : {
            judgeFunc : function(prd_seq_id,mdlIdFk){
                $("#layoutGrd").jqGrid("saveCell",lastrow,lastcell);//保存最后一次单元格的编辑状态
                if(mdlIdFk == ""){
                    showErrorDialog("","产品代码不能为空!");
                    return false;
                }
                var low_ng_limt;
                var inTrxMdl =  {
                    trx_id : VAL.T_FBPMDLDEF,
                    action_flg: "Q",
                    iary: [{
                        mdl_id : mdlIdFk
                    }]
                };
                var outTrxMdl = comTrxSubSendPostJson(inTrxMdl);
                if(outTrxMdl.rtn_code === VAL.NORMAL){
                    var oaryB = outTrxMdl.oary[0].oaryB;
                    if(oaryB.length<1){
                        showErrorDialog("","该玻璃的等级规则不存在!");
                        return false;
                    }
                    for(var i =0 ;i<oaryB.length ; i++){
                        if(oaryB[i].prd_grade === "NG"){
                            low_ng_limt = oaryB[i].low_ng_limt;
                            if(level>=oaryB[i].low_ng_limt && level<=oaryB[i].up_ng_limt){
                                prd_grade=oaryB[i].prd_grade;
                            }
                        }
                    }
                }
                var grade="";
                var level=0;
                var a_grade_cnt =0,b_grade_cnt =0 ,c_grade_cnt= 0,x_grade_cnt,o_grade_cnt;
                var prd_grade,chip_grade;
                var data = $("#layoutGrd").jqGrid("getRowData");
                //根据不同的运行模式，判等的规则不一样
                var tool_run_mode = $("#opeIdSel").val().split("@")[2];
                if(tool_run_mode != "INS" && tool_run_mode != "PACK"){
                    for(var i=0;i<data.length;i++){
                        for(var pos_id in data[i]){
                            if(data[i][pos_id]=='A'){
                                grade+=data[i][pos_id];
                                a_grade_cnt = a_grade_cnt+1;
                            }else if(data[i][pos_id]=='B'){
                                grade+=data[i][pos_id];
                                b_grade_cnt = b_grade_cnt+1;
                            }else if(data[i][pos_id]=='C'){
                                grade+=data[i][pos_id];
                                c_grade_cnt = c_grade_cnt+1;
                            }
                        }
                    }
                    if(b_grade_cnt+a_grade_cnt === 0 && c_grade_cnt != 0){
                        prd_grade = "OK";
                    }else if(a_grade_cnt >= low_ng_limt){
                        prd_grade = "NG";
                    }else{
                        prd_grade = "GRAY";
                    }
                } else{
                    for(var i=0;i<data.length;i++){
                        for(var pos_id in data[i]){
                            if(data[i][pos_id]=='O'){
                                grade+=data[i][pos_id];
                                o_grade_cnt = o_grade_cnt+1;
                            }else if(data[i][pos_id]=='X'){
                                grade+=data[i][pos_id];
                                x_grade_cnt = x_grade_cnt+1;
                                level=level+1;
                            }
                        }
                    }
                }
                chip_grade = grade;
                var repair_flg = $("#repair_flg").val();
                var inspection_flg= $("#inspection_flg").val();
                var abnormal_flg = $("#abnormal_flg").val();
                var ito_cid = $("#ito_cid").val();

                var inObj,outObj;
                var idLens, prd_ids = [], flag;
                var iaryA = new Array();
                //Get box info
                var out_box_id, crCnt, maxCnt,prd_grade;
                if(domObj.$okCrrIdTxt.attr("disabled") != "disabled"){
                    flag = "OK",
                        out_box_id = domObj.$okCrrIdTxt.val().trim();
                    maxCnt = domObj.$OKmaxCntSpan.val();
                    crCnt  = domObj.$OKcurCntSpan.val();
                }else if(domObj.$ngCrrIdTxt.attr("disabled") != "disabled"){
                    flag = "NG";
                    out_box_id = domObj.$ngCrrIdTxt.val().trim();
                    maxCnt = domObj.$NGmaxCntSpan.val();
                    crCnt  = domObj.$NGcurCntSpan.val();
                }else if(domObj.$OthCrrIdTxt.attr("enabled") === "enabled"){
                    flag = "Other";
                    out_box_id = domObj.$OthCrrIdTxt.val().trim();
                    maxCnt = domObj.$OthmaxCntSpan.val();
                    crCnt  = domObj.$OthcurCntSpan.val();
                }
                //check box info
                if(!out_box_id){
                    showErrorDialog("","请输入转入箱号！");
                    return false;
                }
                if(maxCnt == "" || crCnt == ""){
                    showErrorDialog("","请先获取转入箱号信息！");
                    return false;
                }
                var rowIds = domObj.grids.$shtInprListGrd.jqGrid('getGridParam','selarrrow');
                idLens = rowIds.length;
                if(idLens == 0){
                    showErrorDialog("", "请选择需要转入的玻璃！");
                    return false;
                }
                //Max count check
                if(parseInt(maxCnt,10) < parseInt(crCnt,10) + idLens){
                    showErrorDialog("","箱子["+out_box_id+
                        "]当前数量为["+crCnt+"],最大容量为["+maxCnt+
                        "],不能再放入["+idLens+"]片产品！");
                    return false;
                }

                var tool_id = $("#toolIdSel option:selected").val();
                if(!tool_id){
                    showErrorDialog("", "设备代码不能为空！");
                    return false;
                }
                var tool_run_mode = $("#opeIdSel").val().split("@")[2];
                var real_prd_seq_id = $("#real_prd_seq_id").val();
                var cus_sn = $("#cus_sn").val();

                //根据圈叉信息判断能否转入箱子
                var regex_o = new RegExp("O",'g');
                var result_o = chip_grade.match(regex_o);
                var count_o =  !result_o ? 0 : result_o.length;
                var regex_x = new RegExp("X",'g');
                var result_x = chip_grade.match(regex_x);
                var count_x =  !result_x ? 0 : result_x.length;
                var prd_grade_flg;
                if(tool_run_mode === "INS"){
                    if(count_x ===0 && count_x != 0 ){
                        prd_grade_flg = "OK";
                    }else if(count_x >= low_ng_limt){
                        prd_grade_flg = "NG";
                    }else {
                        prd_grade_flg = "Other";
                    }
                    if(prd_grade_flg != flag){
                        showErrorDialog("","id为["+prd_seq_id+"]的玻璃不能 转入到["+prd_grade_flg+"]对应的箱子！");
                        return;
                    }
                }

                var prd_inf={
                    prd_seq_id : prd_seq_id,
                    real_prd_seq_id:real_prd_seq_id,
                    cus_prd_seq_id:cus_sn,
                    prd_grade :prd_grade,
                    chip_grade:chip_grade,
                    repair_flg:repair_flg,
                    inspection_flg:inspection_flg,
                    abnormal_flg:abnormal_flg,
                    ito_cid:ito_cid
                }
                iaryA.push(prd_inf);
                inObj={
                    trx_id  : VAL.T_FPPPNLOUT,
                    box_id  : out_box_id,
                    evt_usr : VAL.EVT_USER,
                    // evt_usr : "CMJ",
                    tool_id : tool_id,
                    prd_cnt : idLens,
                    opi_ope_flg : "Y",
                    iary   : iaryA,

                }
                outObj = comTrxSubSendPostJson(inObj);
                if(outObj.rtn_code == VAL.NORMAL){
                    btnFunc.getBoxInf(flag, out_box_id);
                    for(var i =0;i<rowIds.length;i++){
                        domObj.grids.$shtInprListGrd.jqGrid('delRowData', rowIds[i]);
                        i--;
                    }
                }
                domObj.dialogs.$judgeDialog.modal("hide");
            }
        }
    }
    domObj.$opeIdSel.change(function(){
        controlsFunc.initToolSel();
        controlsFunc.initPortSel();
        controlsFunc.initControls();
    })
    domObj.$toolIdSel.change(function(){
        controlsFunc.initControls();
        controlsFunc.initPortSel();
        controlsFunc.setInprPrdList();
    });
    domObj.$okCrrIdTxt.keydown(function(event){
        if(event.keyCode == 13){
            var box_id = domObj.$okCrrIdTxt.val().trim();
            btnFunc.getBoxInf("OK",box_id);
        }
    })
    domObj.$ngCrrIdTxt.keydown(function(event){
        if(event.keyCode == 13){
            var box_id = domObj.$ngCrrIdTxt.val().trim();
            btnFunc.getBoxInf("NG",box_id);
        }
    })
    domObj.$OthCrrIdTxt.keydown(function(event){
        if(event.keyCode == 13){
            var box_id = domObj.$OthCrrIdTxt.val().trim();
            btnFunc.getBoxInf("Other",box_id);
        }
    });
    var setDialogSelFnc = function(labelObj,flgObj){
        var str = '<div class="form-group col-lg-6 col-md-6 col-sm-6"> <div class="col-lg-5 col-md-5 col-sm-5">'+labelObj +'</div><div class="col-lg-7 col-md-7 col-sm-7"><select type="text" id= '+flgObj+' class="form-control"> <option value="Y">Y</option> <option value="N">N</option> </select></div></div>';
        $(str).appendTo($("#layoutDiv"));
    }
    var setLayoutGridInfo = function(infoCM){
        $("#layoutDiv").empty();
        var tool_run_mode = $("#opeIdSel").val().split("@")[2];
        if(tool_run_mode === "BM" || tool_run_mode ==="B" || tool_run_mode === "AOI" ){
            setDialogSelFnc("修补标识","repair_flg");
            setDialogSelFnc("检测标识","inspection_flg");
        }else if (tool_run_mode === "ITO"){
            setDialogSelFnc("AbnormalFlag","abnormal_flg");
            setDialogSelFnc("ITO CID","ito_cid");
        }
        if(tool_run_mode === "BM"){
            var inputStr = '<div class="form-group col-lg-6 col-md-6 col-sm-6"> <div class="col-lg-5 col-md-5 col-sm-5">实际ID</div><div class="col-lg-7 col-md-7 col-sm-7"> <input type= "text" id = "real_prd_seq_id" class="form-control"/></div></div> <div class="form-group col-lg-6 col-md-6 col-sm-6"> <div class="col-lg-5 col-md-5 col-sm-5">客户ID </div><div class="col-lg-7 col-md-7 col-sm-7"> <input type= "text" id = "cus_sn" class="form-control"/></div></div>';
            $(inputStr).appendTo($("#layoutDiv"));
        }
        var str = ' <div class="layoutGrid col-lg-12 col-md-12 col-sm-12" > <table id="layoutGrd"></table><div id="layoutPg"></div></div>';
        $(str).appendTo($("#layoutDiv"));
        $("#layoutGrd").jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            autoheight:true,
            autowidth : true,
            //  autowidth:true,
            shrinkToFit:true,
            scroll:true,
            rownumWidth : true,
            resizable : true,
            rowNum:40,
            loadonce:true,
            viewrecords:true,
            sortable:false,
            rownumbers  :true ,//显示行号
            rownumWidth : 30,  //行号列宽度
            pager : "#layoutPg",
            fixed: true,
            cellEdit:true,
            colModel: infoCM,
            cellsubmit:'clientArray',
            beforeEditCell:function(rowid,cellname,v,iRow,iCol){
                lastId  = rowid;
                lastrow = iRow;
                lastcell = iCol;
            }

        });
    };

    var iniButtonAction = function(){
        domObj.buttons.$query_btn.click(function(){
            controlsFunc.setInprPrdList();
        });
        domObj.buttons.$OKbtn.click(function(){
            btnFunc.okBtnfunc();
        })
        domObj.buttons.$NGbtn.click(function(){
            btnFunc.ngBtnfunc();
        })
        domObj.buttons.$Othbtn.click(function(){
            btnFunc.OthBtnfunc();
        })
        domObj.buttons.$prdMoveInBox_btn.click(function(){
            btnFunc.swithTofunc();
        })
        domObj.buttons.$moveOut_btn.click(function(){
            btnFunc.moveOutBefore();
        })
        domObj.buttons.$moveOut_btn.click(function() {
            btnFunc.moveOutfunc();
        })
        domObj.buttons.$clear_btn.click(function(){
            btnFunc.clear_func();
        })
    }
    function initFunc(){
        controlsFunc.initControls();
        controlsFunc.initShtInprGirdInfo();
        controlsFunc.initBoxOutGirdInfo();
        controlsFunc.initOpeSelect();
        // controlsFunc.initToolSel();
        controlsFunc.initPortSel();
        controlsFunc.setInprPrdList();
        iniButtonAction();

    }

    initFunc();
    //模态框拖动
    $(".modal-content").draggable();
    //表格自适应
    function resizeFnc(){
        var offsetBottom,divWidth,offsetBottom1,divWidth1;
        //1
        divWidth = $("#divTableLeft").width();
        offsetBottom =  $(window).height() - $("#divTableLeft").offset().top- 295;
        $("#divTableLeft").height(offsetBottom * 0.95);
        $("#shtInprListGrd").setGridWidth(divWidth * 0.30);
        $("#shtInprListGrd").setGridHeight(offsetBottom * 0.99 );
        //2
        // divWidth1 = $(window).width() - $("#boxOutListDiv").offset().left;
        // $("#boxOutListGrd").setGridWidth(divWidth1 * 0.99-38);
        // offsetBottom1 = $("#divLeftT").height();
        // $("#boxOutListDiv").height(offsetBottom1 * 0.99);
        // $("#boxOutListGrd").setGridHeight(offsetBottom1 * 0.99 * 0.9 - 10);
        divWidth1 = $("#rightDiv").width();
        offsetBottom1 =$(window).height() -  $("#rightDiv").offset().top- $("#remarkDiv").height()-51-280;
        $("#rightDiv").height(offsetBottom1 * 0.95);
        $("#boxOutListGrd").setGridWidth(divWidth1 * 0.99);
        $("#boxOutListGrd").setGridHeight(offsetBottom1 * 0.99);

    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });



})
